﻿using DAL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TOOLS;
namespace BLL
{
    public class BaseBll
    {
        private static Queue<string> logQueue = new Queue<string>();
        public static bool enabledLog = true;
        public static Dictionary<string, Model.LoginInfo> _loginUserList = new Dictionary<string, Model.LoginInfo>();
        public static Dictionary<string, Model.LoginInfo> loginUserList
        {
            get 
            {
                lock (_loginUserList)
                { 
                    return _loginUserList;
                }
            }
            set 
            {
                lock (_loginUserList)
                {
                    _loginUserList = value;
                }
            }
        }
        public static Dictionary<string, Model.MLoginInfo> mLoginUserList = new Dictionary<string, Model.MLoginInfo>();
        /// <summary>
        /// 检查登录超时
        /// </summary>
        public static void CheckSigTimeOut()
        {
            Dictionary<string, Model.LoginInfo> list = new Dictionary<string, Model.LoginInfo>(loginUserList);
            List<string> timeoutKeyList = new List<string>();
            foreach (var item in list)
            {
                if (item.Value.endTime.AddMinutes(10) < DateTime.Now)
                {
                    timeoutKeyList.Add(item.Key);
                }
            }
            foreach (var item in timeoutKeyList)
            {
                EnLog(loginUserList[item].ToJSON() + "\r\n登录超时");
                loginUserList.Remove(item);
            }
        }
        public static bool IsLogin(string username)
        {
            return loginUserList.Where(p => p.Value.loginUser.user_name == username).Count()>0;
        }
        public BaseBll()
        {
         
        }
        public static void EnLog(string value,params object[] args)
        {
            
            if (enabledLog)
            {
                if (args.Count()>0)
                {
                    logQueue.Enqueue(DateTime.Now.ToString() + "  " + string.Format(value, args));
                }
                else
                {
                    logQueue.Enqueue(DateTime.Now.ToString() + "  " + value);
                }
            }
        }
        
        public static string DeLog()
        {
            if (logQueue.Count>0)
            {
                return logQueue.Dequeue();
            }
            return null;
        }
       
    }
}
